package club.lovety.code.rocketmq_demo;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * <p>
 * 这里写有象视频代码描述
 * </p>
 * mq的生产者
 *
 * @author sunmc
 * @date created in 2018/5/3 10:09
 * @since
 */
public class ProducerDemo {


    public static void main(String[] args) throws Exception {
//        AsyncProducer();
//        syncProducer();
        produce();
    }

    public static void AsyncProducer() throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();
        producer.setRetryTimesWhenSendAsyncFailed(0);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("ty",
                    "TagA",
                    "OrderID188",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            producer.send(msg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.printf("%-10d OK %s %n", index,
                            sendResult.getMsgId());
                }

                @Override
                public void onException(Throwable e) {
                    System.out.printf("%-10d Exception %s %n", index, e);
                    e.printStackTrace();
                }
            });
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }


    public static void syncProducer() throws Exception {

        DefaultMQProducer producer = new
                DefaultMQProducer("please_rename_unique_group_name");

        producer.setNamesrvAddr("127.0.0.1:9876");

        producer.start();

        for (int i = 0; i < 100; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("smc",
                    "TagA",
                    ("Hello RocketMQ " +
                            i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }

        System.out.println(11);

        producer.shutdown();
    }

    public static void produce() throws Exception {

        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("procude");

        defaultMQProducer.setNamesrvAddr("localhost:9876");

        defaultMQProducer.start();

        for (int i=0;i<10;i++){
            Message message = new Message("mmm","结合springboot使用rocketmq ".getBytes("UTF-8"));
            SendResult sendResult =  defaultMQProducer.send(message);
            System.out.println("发送后的状态: "+sendResult.getSendStatus());
        }
        defaultMQProducer.shutdown();
    }


}
